﻿Public Class Form1

    Private Enum AppSelection
        Folder = 0
        IIS = 1
    End Enum

    Private _selection As AppSelection
    Private _siteToCompile As WebSite = New WebSite()

    Private Sub btnBrowseAppDir_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBrowseAppDir.Click
        Me.FolderBrowser.ShowDialog()
        If Me.FolderBrowser.SelectedPath <> String.Empty Then
            Me.txtApp.Text = Me.FolderBrowser.SelectedPath
            _selection = AppSelection.Folder
        End If
    End Sub

    Private Sub btnBrowseOutputDir_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBrowseOutputDir.Click
        Me.FolderBrowser.ShowDialog()
        If Me.FolderBrowser.SelectedPath <> String.Empty Then
            Me.txtOutput.Text = Me.FolderBrowser.SelectedPath
        End If
    End Sub

    Private Sub btnIIS_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnIIS.Click
        Dim frm As New IIS
        frm.ShowDialog()
        If Not frm.Path = String.Empty Then
            'If frm.Path.StartsWith("IIS:") Then
            Me.txtApp.Text = frm.Path
            _selection = AppSelection.IIS
        End If
        frm.Dispose()
    End Sub


    Private Sub btnCompile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCompile.Click

        If (_selection.Equals(AppSelection.Folder)) Then
            _siteToCompile.OriginalPath = Me.txtApp.Text
        Else
            _siteToCompile.IisPath = Me.txtApp.Text
        End If

        _siteToCompile.OutputPath = Me.txtOutput.Text
        _siteToCompile.OutputName = Me.txtMergeAss.Text
        _siteToCompile.PartialBuild = Me.chkFullCompil.Checked
        _siteToCompile.FixedNames = Me.chkFixedNames.Checked
        _siteToCompile.WriteBatch = Me.chkBatch.Checked

        If (Me.cmbOutputType.Text = "Web Site") Then
            _siteToCompile.OutputType = Output.WebSite
        Else
            _siteToCompile.OutputType = Output.ControlLibrary
        End If

        _siteToCompile.Compile()

    End Sub

    Private Sub MergeDlls()
        'Obtenir le repertoire de windows
        Dim dir As New IO.DirectoryInfo(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles))

        Dim cmd As String = dir.FullName & "\Microsoft\ILMerge\ILMerge.exe"
        Dim args As String = " /ndebug"

        'Chercher dans le repertoire de sortie l'ensemble des assemblys sorties...
        Dim outputDir As String = String.Empty
        If (Not String.IsNullOrEmpty(Me.txtOutput.Text)) Then
            outputDir = Me.txtOutput.Text + "\bin\"
        End If

        Dim files As String() = IO.Directory.GetFiles(outputDir, "*.ascx.*.dll")

        If (files.Length > 0) Then
            args += " /out:""" + Me.txtMergeAss.Text + """"
        End If

        For Each file As String In files
            args += " """ + file + """"
        Next

        Dim myProcess As New System.Diagnostics.Process



        myProcess.StartInfo.Arguments = args
        myProcess.StartInfo.FileName = cmd
        myProcess.StartInfo.WorkingDirectory = dir.ToString
        myProcess.StartInfo.WindowStyle = ProcessWindowStyle.Normal

        myProcess.Start()

    End Sub

    Private Sub chkMerge_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkMerge.CheckedChanged
        EnableMergeAssemblyTextBox()
    End Sub

    Private Sub EnableMergeAssemblyTextBox()
        Me.txtMergeAss.Enabled = Me.chkMerge.Checked
        Me.lblChemin.Enabled = Me.chkMerge.Checked

    End Sub

    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        EnableMergeAssemblyTextBox()
        LoadOutputTypes()
    End Sub

    Private Sub LoadOutputTypes()
        Me.cmbOutputType.Items.Add("Web Site")
        Me.cmbOutputType.Items.Add("Control Library")
        Me.cmbOutputType.SelectedIndex = 0
    End Sub

    Private Sub cmbOutputType_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbOutputType.SelectedIndexChanged
        If (Me.cmbOutputType.Text = "Web Site") Then
            Me.chkBatch.Enabled = True
            Me.chkFixedNames.Enabled = True
            Me.chkFullCompil.Enabled = True
        Else
            Me.chkBatch.Checked = True
            Me.chkBatch.Enabled = False

            Me.chkFixedNames.Checked = True
            Me.chkFixedNames.Enabled = False

            Me.chkFullCompil.Checked = False
            Me.chkFullCompil.Enabled = False

            Me.chkMerge.Checked = True

        End If
    End Sub
End Class
